using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Controllers
{
	#region using
	using System.Web.Mvc;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	using TemplateWeb.Models.Repositories;
	using TemplateWeb.Helpers;
	using System.Web.Security;
	#endregion
	public partial class AdminController: BaseController
	{
		public ActionResult SelectSach()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_SACH
			};
			return View(CreateViewModel(data));
		}


		public ActionResult SelectSachByIDLoaiSach(int idloaisach)
		{
			var data = new DataTransferViewModel
			{
				IDLoaiSach = idloaisach,
				EnumViewModelType = EnumViewModel.ADMIN_SACH,
			};
			return View("SelectSach", CreateViewModel(data));
		}

		public ActionResult SelectSachByIDLoaiSachPaging(int idloaisach)
		{
			var data = new DataTransferViewModel
			{
				IDLoaiSach = idloaisach,
				EnumViewModelType = EnumViewModel.ADMIN_SACH,
			};
			return PartialView("Templates/TH_ListSach", CreateViewModel(data));
		}


		[AcceptVerbs(HttpVerbs.Delete)]
		public JsonResult DeleteSach(int id)
		{
			var deleteItem = _repSach.SelectByID(id);
			var fileName = deleteItem.HinhMinhHoa;
			var idloaisach = deleteItem.IDLoaiSach;
			var result = _repSach.Delete(id);
			if (result)
			{
				if (fileName != "noimages.jpg")
				{
					var fullPath = Server.MapPath("/Content/Images/Items/" + fileName);
					DeleteFile(fullPath);
				}
			}
			return Json(new{
				Success = result,
				RecordCount = _repSach.SelectAll().Count,
				DeleteId = id
			});
		}

		public ActionResult DetailOfSach(int id)
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_SACH,
				IntID = id
			};
			return View(CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateSach(FormCollection forms)
		{
			string id = forms.GetValues("Sach_ID")[0];
			string idloaisach = forms.GetValues("Sach_IDLoaiSach")[0];
			string tensach = forms.GetValues("Sach_TenSach")[0];
			string noidung = forms.GetValues("Sach_NoiDung")[0];
			string noidungrutgon = StringHelper.Truncate(forms.GetValues("Sach_NoiDungRutGon")[0], WebConfiguration.SachSortDescription);
			string tacgia = forms.GetValues("Sach_TacGia")[0];
			string nhaxuatban = forms.GetValues("Sach_NhaXuatBan")[0];
			string kho = forms.GetValues("Sach_Kho")[0];
			string tieubieu = forms.GetValues("Sach_TieuBieu")[0];
			string bestseller = forms.GetValues("Sach_BestSeller")[0];
			var file = Request.Files["Sach_HinhMinhHoa"];

			string refName = string.Empty;
            long gia;
            int sotrang;

            if (!long.TryParse(forms.GetValues("Sach_Gia")[0].Replace(",", string.Empty).Replace(".", string.Empty), out gia))
            {
                gia = 0;
            }

            if (!int.TryParse(forms.GetValues("Sach_SoTrang")[0].Replace(",", string.Empty).Replace(".", string.Empty), out sotrang))
            {
                sotrang = 0;
            }

            var editItem = _repSach.SelectByID(int.Parse(id));

			editItem.IDLoaiSach = int.Parse(idloaisach.Replace(".", string.Empty).Replace(",", string.Empty));
			editItem.TenSach = tensach;
			editItem.NoiDung = noidung;
			editItem.NoiDungRutGon = noidungrutgon;
			editItem.TacGia = tacgia;
            editItem.Gia = gia;
            editItem.SoTrang = sotrang;
			editItem.NhaXuatBan = nhaxuatban;
			editItem.Kho = kho;
			editItem.TieuBieu = bool.Parse(tieubieu.Replace(".", string.Empty).Replace(",", string.Empty));
			editItem.BestSeller = bool.Parse(bestseller.Replace(".", string.Empty).Replace(",", string.Empty));

			var result = false;
			if (file.ContentLength != 0)
			{
				result = UploadFile(file, "/Content/Images/Items/", id, ref refName);
				editItem.HinhMinhHoa = refName;
			}
			result = _repSach.Save();
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_DETAILOF_SACH,
				IntID = int.Parse(id.Replace(".", string.Empty).Replace(",", string.Empty)),
				InfoText = result ? "Item has been updated" : "Cannot update this item"
			};
			return View("DetailOfSach", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertSach(FormCollection forms)
		{
			string idloaisach = forms.GetValues("Sach_IDLoaiSach")[0];
			string tensach = forms.GetValues("Sach_TenSach")[0];
			string noidung = forms.GetValues("Sach_NoiDung")[0];
			string noidungrutgon = StringHelper.Truncate(forms.GetValues("Sach_NoiDungRutGon")[0], WebConfiguration.SachSortDescription);
			string tacgia = forms.GetValues("Sach_TacGia")[0];
			string nhaxuatban = forms.GetValues("Sach_NhaXuatBan")[0];
			string kho = forms.GetValues("Sach_Kho")[0];
			string tieubieu = forms.GetValues("Sach_TieuBieu")[0];
			string bestseller = forms.GetValues("Sach_BestSeller")[0];
			var file = Request.Files["Sach_HinhMinhHoa"];

            long gia;
            int sotrang;

            if (!long.TryParse(forms.GetValues("Sach_Gia")[0].Replace(",", string.Empty).Replace(".", string.Empty), out gia))
            {
                gia = 0;
            }

            if (!int.TryParse(forms.GetValues("Sach_SoTrang")[0].Replace(",", string.Empty).Replace(".", string.Empty), out sotrang))
            {
                sotrang = 0;
            }

			string refName = string.Empty;
			var newItem = new Sach
			{
				IDLoaiSach = int.Parse(idloaisach),
				TenSach = tensach,
				NoiDung = noidung,
				NoiDungRutGon = noidungrutgon,
				TacGia = tacgia,
				Gia = gia,
				SoTrang = sotrang,
				NhaXuatBan = nhaxuatban,
				Kho = kho,
				TieuBieu = bool.Parse(tieubieu),
				BestSeller = bool.Parse(bestseller),
				HinhMinhHoa = "noimages.jpg",
                NgayDang = DateTime.Now
			};

			var result = _repSach.Insert(newItem);

			if (result)
			{
				if (file.ContentLength != 0)
				{
					result = UploadFile(file, "/Content/Images/Items/", newItem.ID.ToString(), ref refName);
					if (result)
					{
						var addedItem = _repSach.SelectByID(newItem.ID);
						addedItem.HinhMinhHoa = refName;
						_repSach.Save();
					}
				}
			}
			var data = new DataTransferViewModel
			{
				IDLoaiSach = int.Parse(idloaisach),
				EnumViewModelType = EnumViewModel.ADMIN_SACH,
				InfoText = result ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectSach", CreateViewModel(data));
		}


	}
}